package com.dtw.ditouwang.ui.more;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.dtw.ditouwang.R;
import com.dtw.ditouwang.beans.MoreBean;
import com.dtw.ditouwang.ui.BaseFragment;
import com.dtw.ditouwang.ui.OnItemClickListener;
import com.dtw.ditouwang.ui.mine.MyCollectionActivity;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MoreForYouFragment extends BaseFragment implements OnItemClickListener {
    EditorChoosePhotoAdapter editorChoosePhotoAdapter;
    List<MoreBean.DataBean> dataBeanList = new ArrayList<>();

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.activity_editor_choose_photo, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        view.findViewById(R.id.tablayout).setVisibility(View.GONE);
        view.findViewById(R.id.titleview).setVisibility(View.GONE);
        RecyclerView recyclerView = view.findViewById(R.id.recyclerview);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        editorChoosePhotoAdapter = new EditorChoosePhotoAdapter(dataBeanList);
        editorChoosePhotoAdapter.setOnItemClickListener(this);
        recyclerView.setAdapter(editorChoosePhotoAdapter);
    }

    @Override
    public void onViewStateRestored(@Nullable Bundle savedInstanceState) {
        super.onViewStateRestored(savedInstanceState);
    }

    @Override
    public void onStart() {
        super.onStart();
        webApi.getMore(2).enqueue(new Callback<MoreBean>() {
            @Override
            public void onResponse(Call<MoreBean> call, Response<MoreBean> response) {
                if (response.body().isResult()&&getActivity()!=null&&!getActivity().isFinishing()) {
                    dataBeanList.clear();
                    dataBeanList.addAll(response.body().getData());
                    editorChoosePhotoAdapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onFailure(Call<MoreBean> call, Throwable t) {

            }
        });
    }

    @Override
    public void onItemClick(int viewID, int position, int state) {
        switch (viewID) {
            case R.id.img_head_frist:
                MyCollectionActivity.start(getContext(),dataBeanList.get(position).getUser_id());
                break;
            case R.id.image_image:
                Intent nor = new Intent(getContext(), EditorChooseNormalDetailActivity.class);
                nor.putExtra(EditorChooseNormalDetailActivity.KeySYSID, dataBeanList.get(position).getId());
                startActivity(nor);
                break;
            default:
                Intent intent = new Intent(getContext(), EditorChoosePhotoDetailActivity.class);
                intent.putExtra(EditorChoosePhotoDetailActivity.KeyID, dataBeanList.get(position).getId());
                startActivity(intent);
                break;
        }
    }
}
